package com.boc.ewr.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.*;
import com.boc.ewr.bcimp.service.*;
import com.boc.ewr.business.entity.AcctUncaDtl;
import com.boc.ewr.business.entity.vo.CaUncaDtlRespVo;
import com.boc.ewr.business.mapper.AcctUncaDtlMapper;
import com.boc.ewr.business.service.IAcctUncaDtlService;
import com.boc.ewr.util.MsgCategoryEnum;
import com.boc.ewr.util.ToUserUtil;
import com.boc.ewr.util.file.enumeration.CaAccountStatusEnum;
import com.boc.ewr.work.entity.WorkRecordDtl;
import com.boc.ewr.work.service.IWorkRecordDtlService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.prefs.BackingStoreException;

/**
 * @Description: 未对账账户明细
 * @Author: jeecg-boot
 * @Date: 2023-05-26
 * @Version: V1.0
 */
@Service
public class AcctUncaDtlServiceImpl extends ServiceImpl<AcctUncaDtlMapper, AcctUncaDtl> implements IAcctUncaDtlService {

    @Autowired
    private ICacService cacService; //CAC

    @Autowired
    private IWorkRecordDtlService workRecordDtlService; //工作记录明细

    @Autowired
    private ISysBaseAPI sysBaseAPI;

     @Autowired
     private AcctUncaDtlMapper acctUncaDtlMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        // 清除之前数据
        this.remove(null);
        //生成数据
        List<AcctUncaDtl> info = acctUncaDtlMapper.createInfo();
        return this.saveBatch(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void fdbkAndAddWorkRecord(CaUncaDtlRespVo caUncaDtlRespVo) {
        // 更新是否已反馈
        AcctUncaDtl acctUncaDtl = new AcctUncaDtl();
        BeanUtils.copyProperties(caUncaDtlRespVo, acctUncaDtl);

        //acctUncaDtl.setIsFdbk(YnEnum.YES.getIndex()); 确认是否需要已反馈字段

        // 更新工作记录
        WorkRecordDtl workRecordDtl = new WorkRecordDtl();
        BeanUtils.copyProperties(caUncaDtlRespVo, workRecordDtl);

        LoginUser loginUser = this.getLoginUser();
        String orgCode = loginUser.getOrgCode();
        workRecordDtl.setParentBranchNo(sysBaseAPI.getParentDepartId(orgCode).getText());
        workRecordDtl.setOrgNo(orgCode);
        workRecordDtl.setOrgNm(sysBaseAPI.selectAllById(orgCode).getDepartName());
        workRecordDtl.setEmpNm(loginUser.getRealname());

        // 同对账单号和同协议号下存在不同币种对应的账号，此处不涉及账号，因此可直接取第一个
        Cac cac = cacService.list(
                new LambdaQueryWrapper<Cac>()
                        .eq(Cac::getCaNo, workRecordDtl.getCaNo())
                        .eq(Cac::getCaProtocolNo, workRecordDtl.getProtocolNo())
        ).get(0);

        workRecordDtl.setCusmAddr(cac.getAccSendAddr());
        workRecordDtl.setContactNm(cac.getLinkerName());
        workRecordDtl.setContactMob(cac.getLinkerMobile());
        workRecordDtl.setEhrTlrNo(sysBaseAPI.getWorkNoByLoginUser(loginUser));

        workRecordDtlService.save(workRecordDtl);
    }

    // 获取登录角色
    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }
}
